// pages/new_lamp/new_lamp.js
var app=getApp()
let header = {
  'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
  'cookie': "openid=" + app.globalData.openid
};
Page({
  /**
   * 页面的初始数据
   */
  data: {
    auto:false,
    longitude:'',
    latitude:'',
    loadgroupmid: '',
    loadgroupid: '',
    loadname: '',
    districtid:'',
    districtmid: '',
    districtname: '',

    lampname:'',
    lampbrand:'',
    lampno:'',
    lamppower:0,
    controllerno:'',
    poweron_time:'00:01',
    poweroff_time:'23:59',
    maxelec:0,
    minelec:0,
    index:0,
    groupID:-1,
    lamp_type_array: [
      {
        id:"0",
        name:"普通灯"
      },
      {
        id:"1",
        name:"十字路口灯"
      },
      {
        id:"2",
        name:"主干道灯"
      },
      {
        id:"3",
        name:"人行道灯"
      },
      {
        id:"4",
        name:"隧道灯"
      }
    ],
    index_modal:0,
    modal_type_array: [
      
    ],
    is_show_createModal:false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    //请求时间模式
    wx.request({
      url: "https://119.3.46.145/server/GetLightModal",
      type: "post",
      data: {
        userid: app.globalData.userid
      },
      success: function (res) {        
         //data: "[{"turnOff":"23:59","turnOffChange":"20","turnOn":"00:01","name":"模板1","id":"2","turnOnChange":"-10"},{"turnOff":"auto",//"turnOffChange":"0","turnOn":"auto","name":"测试3","id":"4","turnOnChange":"0"},{"turnOff":"07:30:00","turnOffChange":"10","turnOn":"06:03:00",//"name":"测试2-1","id":"5","turnOnChange":"-10"}]"          
          let data = JSON.parse(res.data.data);

          if(data.length == 0){
            that.setData({
              is_show_createModal:true
            });

            return;
          }
          let tmp_modal_type_array = [];

          data.forEach(function(item, index){
            let tmp_item = {};
            tmp_item.id = item.id;
            tmp_item.name = item.name;
            tmp_modal_type_array.push(tmp_item);
          });

          that.setData({
            modal_type_array: tmp_modal_type_array
          });      
        }
      });

    that.setData({
      loadgroupmid: options.loadgroupmid,
      loadgroupid: options.loadgroupid,
      loadname: options.loadname,
      districtid: options.districtid,
      districtmid: options.districtmid,
      districtname: options.districtname
    });
  },

  choose_tude: function () {
    let that = this;

    wx.chooseLocation({     
      success(res_location) {       
        that.setData({        
          longitude: res_location.longitude,
          latitude: res_location.latitude,
          //name: res_location.name
        });
      }
    })   
  },

  bindpoweronChange:function(e){
    let that = this;

    that.setData({
      poweron_time:e.detail.value
    });
  },

  bindpoweroffChange:function(e){
    let that = this;
    that.setData({
      poweroff_time:e.detail.value
    });
  },

  bindPickerChange:function(e){
    let that = this;
    that.setData({
      index:e.detail.value
    });
  },

  bindModalChange:function(e){
    let that = this;
    that.setData({
      index_modal:e.detail.value
    });
  },

  formSubmit: function (e) {
    let that = this;    
    let header = {
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
      // 'cookie': "openid=" + app.globalData.openid
    }

    let long_lati = e.detail.value.input_long_lati;
    let long_lati_arr = long_lati.split(",");    

    if (e.detail.value.input_lampname == "") {
      wx.showModal({
        title: '提示',
        content: '单灯名称不能为空',
        showCancel: false
      });

      return;
    }

    if (e.detail.value.controllerno == "") {
      wx.showModal({
        title: '提示',
        content: '控制器编号不能为空',
        showCancel: false
      });

      return;
    }

    if (long_lati_arr[0] == "" || long_lati_arr[1] == "") {
      wx.showModal({
        title: '提示',
        content: '经度或者纬度不能为空',
        showCancel: false
      });

      return;
    }

    //e.detail.value.
    /**input_brand: ""
    input_controllerno: "868221047652659"
    input_lampname: "testlamp3"
    input_lampno: ""
    input_long_lati: "119.39823,32.37654"
    input_maxelec: ""
    input_minelec: ""
    input_power: ""
    picker_poweroff_time: "07:00"
    picker_poweron_time: "06:00"
    picker_type: "1"*/

    //console.log(e.detail.value.input_lampname, "e new_lamp");
    that.setData({
      lampname: e.detail.value.input_lampname,
      lampbrand: e.detail.value.input_brand,
      lampno: e.detail.value.input_lampno,
      lamppower: e.detail.value.input_power,
      controllerno: e.detail.value.input_controllerno,
      poweron_time: e.detail.value.picker_poweron_time,
      poweroff_time: e.detail.value.picker_poweroff_time,
      maxelec: e.detail.value.input_maxelec,
      minelec: e.detail.value.input_minelec,
      index: e.detail.value.picker_type,
      longitude: long_lati_arr[0],
      latitude: long_lati_arr[1],
      groupID: that.data.loadgroupmid,
      auto: e.detail.value.switch,
      index_modal: e.detail.value.picker_modal,
      modalID: that.data.modal_type_array[e.detail.value.picker_modal].id
    });

    //console.log(e.detail.value.picker_modal, 'picker_modal');
    //console.log(that.data.modal_type_array[e.detail.value.picker_modal].id, "modal id");

    //let tmp_lamp_type = that.data.lamp_type_array[that.data.index].name;

    

    wx.request({
      url:'https://119.3.46.145/server/AddSingleLight',
      // url: 'https://119.3.46.145/server/AddSingleLight?userid=' + app.globalData.userid + '&devNo=' + that.data.controllerno + '&Name=' + that.data.lampname + '&LightBrand=' + that.data.lampbrand + '&LightNo=' + that.data.lampno + '&groupID=' + that.data.loadgroupmid + '&RatedPower=' + that.data.lamppower + '&LightType=' + that.data.index + '&Longitude=' + that.data.longitude + '&Latitude=' + that.data.latitude + '&ontime=' + that.data.poweron_time + '&offtime=' + that.data.poweroff_time + '&maxCurrent=' + that.data.maxelec + '&minCurrent=' + that.data.minelec + '&auto=' + that.data.auto+'&modalID='+that.data.modalID,     
      method: 'Post',   
      data:{
        userid:app.globalData.userid,
        devNo:that.data.controllerno,
        Name:that.data.lampname,
        LightBrand:that.data.lampbrand,
        LightNo:that.data.lampno,
        groupID:that.data.loadgroupmid,
        RatedPower:that.data.lamppower,
        LightType:that.data.index,
        Longitude:that.data.longitude,
        Latitude:that.data.latitude,
        ontime:that.data.poweron_time,
        offtime:that.data.poweroff_time,
        maxCurrent:that.data.maxelec,
        minCurrent:that.data.minelec,
        auto:that.data.auto,
        modalID:that.data.modalID        
      },
      header:header,
      success: res => {
        //data: { error: "0", info: "添加成功" }  {error: "-1", info: "已存在，无法添加"}        
        let result = res.data;

        if (result.error == "0") {
          wx.showModal({
            title: '提示',
            content: result.info,
            success: function () {
              wx.request({
                //url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid + '&id=' + that.data.districtid,  //获取区域列表构造树
                url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid + '&id=' + that.data.loadgroupid,
                header: header,
                success: function (res) {
                  let data = JSON.parse(res.data.data);

                  if (data.length == 0) {
                    return;
                  }

                  let tmp_lamp_list = [];

                  data.forEach(function (item, index, self) {
                    if (item.attributes.devType == 0) {
                      tmp_lamp_list.push(item);
                    }
                  });
                  //console.log(tmp_lamp_list, 'tmp_lamp_list');

                  let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
                  let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值
                  prevPage.setData({
                    lamp_list: tmp_lamp_list
                  });
                  wx.navigateBack(); //关闭当前页面，返回上一个页面
                },
                fail: function (error) {
                  console.log(error, "dev_manager  onload loadTree");
                }
              });
            }
          });
        }

        if (result.error == "-1") {
          wx.showModal({
            title: '提示',
            content: result.info,
            showCancel: false
          })
        }
      },
      fail: function (error) {
        console.log(error, "addDepartment error");
      }
    })
  },

  goto_time_template:function(){
    let that = this;
    wx.navigateTo({
      url: '../onoff_manager/onoff_manager',
    })

  },

  formReset: function (e) {
    let that = this;
    that.setData({
      loadgroupName: '',
      describe: '',
      name: '',
      telephone: '',
      longitude: '',
      latitude: '',
    });
  },

  set_auto: function (e) {
    let that = this;
    //console.log(e,"e set_auto");
    if (e.detail.value === true) {
      that.setData({
        isdisabled: true
      });
    }

    if (e.detail.value === false) {
      that.setData({
        isdisabled: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})